//
//  GETimeDifference.m
//  OMS
//
//  Created by goldeney on 14-10-14.
//  Copyright (c) 2014年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "GETimeDifference.h"

@implementation GETimeDifference

/**
 * 计算指定时间与当前的时间差
 * @param compareDate   某一指定时间
 * @return 多少(秒or分or天or月or年)+前 (比如，3天前、10分钟前)
 */
+(NSString *)compareCurrentTime:(NSDate*)compareDate
{
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
}

/**
 * 计算指定时间与当前的时间差
 * @param compareDate   某一指定时间
 * @return 多少(秒or分or天or月or年)+前 (比如，3天前、10分钟前)
 */
+(NSString *)compareCurrentNewTime:(NSDate*)compareDate
{
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    NSString *result;
    
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    
    else{
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"HH:mm"];
        result = [dateFormatter stringFromDate:compareDate];
    }
    
    return  result;
}


/**
 * 计算指定时间与当前的时间差
 * @param compareDate   某一指定时间
 * @return
 */
+(NSString *)compareCurrentChangeTime:(NSDate*)compareDate
{
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    
    if((temp = timeInterval/60/60/24) < 1){
        result = @"今天";
    }
    
    else if((temp = timeInterval/24) <2 && (temp = timeInterval/24) >= 1){
        result = @"昨天";
    }
    
    else if((temp = temp/30) <12){
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM-dd"];
        result = [dateFormatter stringFromDate:compareDate];
    }
    
    else{
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM"];
        result = [dateFormatter stringFromDate:compareDate];
    }
    
    return  result;
}

/**
 * 时间比较
 */
+ (BOOL)compareFirstDatae:(NSDate*)firstDate secondDate:(NSDate*)secondDate
{
    NSTimeInterval _fitstDate = [firstDate timeIntervalSince1970];
    NSTimeInterval _secondDate = [secondDate timeIntervalSince1970];
    if (_fitstDate - _secondDate >= 0) return YES;
    else return NO;
}

@end
